package com.zhang.common.db;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.datasource.DataSourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 数据源构造 DruidDataSourceBuilder
 *
 * @author zhangxin
 * @date 2021/7/1
 */
@Component
@ConfigurationProperties(
        prefix = "jdbc.druid.datasource"
)
public class ExtraDataSourceBuilder implements IDataSourceBuilder {
    private static final Logger logger = LoggerFactory.getLogger(ExtraDataSourceBuilder.class);
    private Map<String, String> props = new HashMap();
    private Map<String, DataSource> cache = new ConcurrentHashMap();
    private String mapperDir;

    public ExtraDataSourceBuilder() {
    }

    public DataSource build(Map<String, String> properties) {
        Map<String, String> dbProp = new HashMap();
        dbProp.putAll(this.props);
        if (properties != null) {
            dbProp.putAll(properties);
        }

        String url = (String)this.props.get("url");
        if (this.cache.containsKey(url)) {
            return (DataSource)this.cache.get(url);
        } else {
            try {
                DataSource dataSource = DruidDataSourceFactory.createDataSource(dbProp);
                this.cache.put(url, dataSource);
                return dataSource;
            } catch (Exception var5) {
                logger.error(var5.getMessage(), var5);
                return null;
            }
        }
    }

    public DataSource build() {
        return this.build(Collections.emptyMap());
    }

    public DataSource build(String dbName) {
        String url = (String)this.props.get("url");
        if (url == null) {
            throw new DataSourceException("database url is empty.");
        } else {
            url = url.replace("{db}", dbName);
            Map<String, String> prop = new HashMap();
            prop.put("url", url);
            return this.build((Map)prop);
        }
    }

    public DataSource build(String dbAddress, String userName, String password, String params, String dbName) {
        Map<String, String> props = new HashMap();
        String jdbcUrl = dbAddress + "/" + dbName;
        if (params != null) {
            jdbcUrl = jdbcUrl + jdbcUrl + "?" + params;
        }

        props.put("url", jdbcUrl);
        props.put("username", userName);
        props.put("password", password);
        String driver = this.getDriver(dbAddress);
        if (driver != null) {
            props.put("driverClassName", driver);
        }

        return this.build((Map)props);
    }

    public Map<String, String> getProps() {
        return this.props;
    }

    public void setProps(Map<String, String> props) {
        this.props = props;
    }

    public String getMapperDir() {
        return mapperDir;
    }

    public void setMapperDir(String mapperDir) {
        this.mapperDir = mapperDir;
    }
}

